package com.cip.admin.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cip.admin.model.Friends;
import com.cip.admin.model.User;
import com.cip.admin.service.FriendsService;
import com.cip.admin.dao.FriendsDAO;
import com.cip.common.util.SecurityUtil;

public class FriendsServicImpl implements FriendsService {
	
	private FriendsDAO friendsDAO;
	
	public Friends saveOrUpdateFriends(Friends friends) throws Exception{
		return friendsDAO.saveOrUpdateFriends(friends);
	}
	
	public List<User> getAllFriendsById( int requestFriendId) throws Exception{
		return friendsDAO.getAllFriendsById(requestFriendId);
	}
	
	public List<User> getAllInactiveFriendsById(int requestFriendId) throws Exception{
		return friendsDAO.getAllInactiveFriendsById(requestFriendId);
	}
	
	public List<User> getAllUnConfirmedFriendsById( int responseFriendId) throws Exception{
		return friendsDAO.getAllUnConfirmedFriendsById(responseFriendId);
	}
	
	public List<User> getSuggestedFriendsList(User requestFriend) throws Exception{
		List<User> searchFriends = new ArrayList<User>();
		searchFriends = friendsDAO.getSuggestedFriendsList(requestFriend);
		searchFriends = removeExistingFriends(searchFriends);
		return searchFriends;		
	}
	
	public List<User> getFriendBySearchName(String name) throws Exception{
		List<User> searchFriends = new ArrayList<User>();
		searchFriends = friendsDAO.getFriendBySearchName(name);
		searchFriends = removeExistingFriends(searchFriends);
		return searchFriends;
	}
	
	public Friends getFriendByRequestAndResponse(int requestFriendId, int responseFriendId) throws Exception{
		return friendsDAO.getFriendByRequestAndResponse(requestFriendId, responseFriendId);
	}
	
	/*public List<Map<String, String>> getAllFriendsAsMap( int requestFriendId) throws Exception{
		List<Friends> friendsList = null;//getAllFriendsById(requestFriendId);
		List<Map<String, String>> listOfMapFriends = new ArrayList<Map<String,String>>();
		for(Friends friends : friendsList ){
			 Map map = new HashMap<String, String>();
			 map.put(friends.getResponseFriend().getUsername(), friends.getResponseFriend().getProfilePath());
			 //map.put("status", friends.getStatus());
			// map.put("userId", friends.getResponseFriend().getId());
			 System.out.println("the friennds details are=====>"+friends.getResponseFriend().getUsername()+friends.getResponseFriend().getProfilePath());
			 listOfMapFriends.add(map);
		}
		 
		return listOfMapFriends;
	}*/
	
	public List<User> removeExistingFriends(List<User> searchFriendsList) throws Exception{
		User loginUser = SecurityUtil.getThreadLocalUser().get();
		List<User> userList = getAllFriendsById(loginUser.getId());
		userList.addAll(getAllInactiveFriendsById(loginUser.getId()));
		searchFriendsList.remove(loginUser);
		for(User user : userList ){
			if(searchFriendsList.contains(user)){
				searchFriendsList.remove(user);
			}
		}		 
		return searchFriendsList;	
	}
	
	
	public List<Map<String, String>> convertFriendsAsMap(List<User> userList) throws Exception{
		System.out.println("comin  in friendservice====>"+userList.size());
		List<Map<String, String>> listOfMapUsers = new ArrayList<Map<String,String>>();		
		for(User user : userList ){
			 Map<String, String> map = new HashMap<String, String>();
			 System.out.println("asasfas====="+user.getUsername()+user.getProfilePath());
			 map.put("user", user.getUsername());
			 map.put("path", user.getProfilePath());
			 listOfMapUsers.add(map);
		}
		return listOfMapUsers;		
	}
	

	public FriendsDAO getFriendsDAO() {
		return friendsDAO;
	}

	public void setFriendsDAO(FriendsDAO friendsDAO) {
		this.friendsDAO = friendsDAO;
	}

}
